package ups;

import ups.MapParse.*;
import ups.Common.*;
import ups.Database.*;
import ups.Wind.*;
import ups.SOAP.*;
import ups.Schedule.*;
import ups.Analysis.*;


import java.io.FileInputStream;
import java.io.File;

import java.util.ArrayList;
import java.util.Hashtable;

import java.util.Enumeration;
import java.net.URL;

public class UPSFerry {
	/*	
		This is the file that runs the rest of the project.
		This program is modal, IE it runs in one of a few modes. That means you can't use it to say build the schedule and OCR an image in one pass. Trust us, it's better this way.
		
		Here are the flags which apply to all of the modes
		
		--debug
			Print debugging information
		--printOnly
			Only print results, do not upload information to the database
		--displayDB
			Prints the string and location of the database, as well as the password.
		
		Modes:
		findShips imageFile letterPatternFolder possibleShipNamesFile
		If the database optio`ns are specified, they must ALL be specified.
		buildSchedule scheduleURLFile
		
		
	 */
	
	public static String DBHostName  = "localhost";
	public static int 	 DBPort		 =	3306;
	public static String DBName		 = "upsferry";
	public static String DBUsername	 = "scanfield";
	public static String DBPass		 = "tiger";
	
	private static String helpMode			= "HELP";
	private static String findShipMode		= "SHIP";
	private static String buildScheduleMode = "SCHEDULE";
	private static String windMode 			= "WIND";
	private static String weatherMode		= "WEATHER";
	private static String debugMode			= "DEBUG";
	
	public static void main(String[] args) {
		Enumeration names = System.getProperties().propertyNames();
		
		while( names.hasMoreElements() ) {
			String name = (String)names.nextElement();
		//	System.out.println( name + " = " + System.getProperty( name ) );
		}
		
		if( args.length == 0 ) {
			System.out.println( "No arguments specified. Printing Help...");
			System.exit(-1);
		}
		String mode =  args[0];
		
		String[] trimmedArgs = new String[ args.length - 1 ];
		int i = 0;
		for( i = 1; i < args.length; i++ ) {
			trimmedArgs[ i - 1 ] = args[ i ];
		}
		
		ArgumentParser argParse = new ArgumentParser( trimmedArgs );
		
		if( mode.equals( helpMode )) {
			String [] helpString = loadFileWithDelimiter( "ups/Help.txt", "\n");
			for( String h : helpString ) {
				System.out.println( h );
			}
		} else if( mode.equals( findShipMode )) {
			String[] files;
			
			if( argParse.valueForKey("--fileList") != null ) {
				files = loadFileWithDelimiter( argParse.valueForKey("--fileList"), " ");
			}
			else if( argParse.valueForKey("--file") != null ) {
				files = new String[1];
				files[0] = ( argParse.valueForKey("--file"));
			} else {
				files = null;
			}

			ShipManager manager = new ShipManager( 	files,
													argParse.valueForKey("--hint"),
													argParse.valueForKey("--letter"),
													argParse.valueForKey("--offset"),
													null );
			
			if( argParse.valueForKey("--save") != null) {
				ShipDatabase shipDB = new ShipDatabase( "localhost", 3306, "ferry", "scanfield", "tiger");
				
				for( Ship s : manager.getShips() ) {
					shipDB.putShipLocation( s, s.getDate() );
				}
			}
		} else if( mode.equals( buildScheduleMode )) {
			ScheduleBuilder sb = new ScheduleBuilder( loadFileWithDelimiter( argParse.valueForKey("--file"), "\n"));
			
			ScheduleDatabase schedDB = new ScheduleDatabase( DBHostName ,DBPort, DBName, DBUsername, DBPass);
			schedDB.putItems(  sb.getSchedule() );
			
			
		} else if( mode.equals( windMode )) {
			WindManager windManager = new WindManager( new File(argParse.valueForKey("--file")) );
			
			if( argParse.valueForKey("--print") != null ) {
				windManager.printResults();
			}
		} else if( mode.equals( weatherMode )) {
			Client c = new Client();
			
			try {
			c.sendMessage( new URL("http://www.weather.gov/forecasts/xml/SOAP_server/"), new Message() );
			} catch ( Exception e ) {
				e.printStackTrace();
			}
		} else if( mode.equals( debugMode )) {
			DebugPrintStream ps = new DebugPrintStream( System.out, false );
			
			System.setOut( ps );
			
			ShipDatabase shipDB = new ShipDatabase( "localhost", 3306, "ferry", "scanfield", "tiger");
			ArrayList<Ship> ships = shipDB.getShips();
			
			ScheduleBuilder sb = new ScheduleBuilder( loadFileWithDelimiter( argParse.valueForKey("--file"), "\n"));
			ArrayList<ScheduleItem> schedule = sb.getSchedule();
			
			Hashtable<String,Point> ports = new Hashtable<String,Point>();
			ports.put( "Bremerton",			new Point( 109, 244 ));
			ports.put( "Seattle", 			new Point( 372, 194 ));
			ports.put( "Bainbridge Island", new Point( 216, 164 ));
			ports.put( "Vashon", 			new Point( 253, 315 ));
			ports.put( "Fauntleroy",		new Point( 318, 299 ));
			ports.put( "Southworth",		new Point( 224, 313 ));
			
			ps.enablePrint();
			
			System.out.println( ships.toString() );
			System.out.println( schedule.toString() );
			System.out.println( ports.toString() );
			
	//		TardyShipAnalysis analysis = new TardyShipAnalysis( ships, schedule, ports );
			
		}
	}
	
	/* Convienence method to load a text file into an array of Strings */
	private static String[] loadFileWithDelimiter( String filePath, String delimiter ) {
		
		try {
			FileInputStream input = new FileInputStream( filePath );
			byte b[] = new byte[ input.available() ];
			input.read(b);
			input.close();
			String fileAsString = new String( b );
			return fileAsString.split( delimiter );
		} catch( Exception e ) {
			System.out.println( filePath + " " + delimiter );
			e.printStackTrace();
			System.exit(-1);
		}
		return null;
	}
}